package com.example.myapplication.viewmodelsharedcommondata.vd2;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.example.myapplication.MyApplication;

public class BaseActivity extends AppCompatActivity {


    public ProjectViewModel projectViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //  一个  Activity  里面的所有  Fragment  都可以共享同一个数据  同步
        //  整个项目只有一份
        projectViewModel = getAppViewModelProvider().get(ProjectViewModel.class);
    }


    /**
     * 给些  BaseActivity  得到  ViewModelProvider
     *
     * @return
     */
    private ViewModelProvider getAppViewModelProvider() {

        return MyApplication.getViewModelProvider(this);
    }

    /**
     * 暴露给子项用  得到 ViewModelProvider
     *
     * @param activity
     * @return
     */
    private ViewModelProvider getActivityViewModelProvider(AppCompatActivity activity) {

        return new ViewModelProvider(activity, activity.getDefaultViewModelProviderFactory());
    }

}